package com.sky.mapper;


import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface AddressMapper {

    /**
     * 新增地址
     * @param address
     */
    @Insert("insert into address_book" +
                   "        (user_id, consignee, phone, sex, province_code, province_name, city_code, city_name, district_code," +
                   "         district_name, detail, label, is_default)" +
                   "        values (#{userId}, #{consignee}, #{phone}, #{sex}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}," +
                   "                #{districtCode}, #{districtName}, #{detail}, #{label}, #{isDefault})")
    void insert(AddressBook address);

    /**
     * 查询地址列表
     * @param addressBook
     * @return
     */
    List<AddressBook> list(AddressBook addressBook);

    /**
     * 删除地址
     * @param address
     */
    @Delete("delete from address_book where id = #{id} and user_id = #{userId}")
    void delete(AddressBook address);

    /**
     * 设置全部为非默认
     * @param address
     */
    @Update("update address_book set is_default = 0 where user_id = #{userId}")
    void cancelDefaultAll(AddressBook address);

    /**
     * 设置默认
     * @param addressBook
     */
    @Update("update address_book set is_default = 1 where id = #{id} and user_id = #{userId}")
    void setDefault(AddressBook addressBook);

    /**
     * 修改地址
     * @param address
     */
    void update(AddressBook address);

    /**
     * 查询地址详情
     * @param addressBook
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook getDetail(AddressBook addressBook);

    /**
     * 查询默认地址
     * @param userId
     * @return
     **/
    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook getDefault(Long userId);

    @Select("select * from address_book where id = #{id} ")
    AddressBook getDetail2(AddressBook addressBook);
}
